package com.copd.controller;

import com.copd.common.Result;
import com.copd.dto.UserRoleDTO;
import com.copd.entity.Role;
import com.copd.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户角色管理")
@RestController
@RequestMapping("/api/user-roles")
@PreAuthorize("hasRole('ADMIN')")
public class UserRoleController {
    
    @Autowired
    private RoleService roleService;
    
    @ApiOperation("获取用户的角色")
    @GetMapping("/user/{userId}")
    public Result<List<Role>> getRolesByUserId(@PathVariable Integer userId) {
        List<Role> roles = roleService.getRolesByUserId(userId);
        return Result.success("获取用户角色成功", roles);
    }
    
    @ApiOperation("为用户分配角色")
    @PostMapping("/user/{userId}/role/{roleId}")
    public Result<Boolean> assignRoleToUser(@PathVariable Integer userId, @PathVariable Integer roleId) {
        boolean assigned = roleService.assignRoleToUser(userId, roleId);
        if (assigned) {
            return Result.success("分配角色成功", true);
        }
        return Result.error("500", "分配角色失败");
    }
    
    @ApiOperation("从用户移除角色")
    @DeleteMapping("/user/{userId}/role/{roleId}")
    public Result<Boolean> removeRoleFromUser(@PathVariable Integer userId, @PathVariable Integer roleId) {
        boolean removed = roleService.removeRoleFromUser(userId, roleId);
        if (removed) {
            return Result.success("移除角色成功", true);
        }
        return Result.error("500", "移除角色失败");
    }
    
    @ApiOperation("更新用户角色")
    @PutMapping("/user/{userId}/roles")
    @Transactional
    public Result<Boolean> updateUserRoles(@PathVariable Integer userId, @RequestBody List<Integer> roleIds) {
        // 首先移除用户的所有角色
        List<Role> userRoles = roleService.getRolesByUserId(userId);
        for (Role role : userRoles) {
            roleService.removeRoleFromUser(userId, role.getRoleId());
        }
        
        // 然后添加新的角色
        if (roleIds != null && !roleIds.isEmpty()) {
            for (Integer roleId : roleIds) {
                roleService.assignRoleToUser(userId, roleId);
            }
        }
        
        return Result.success("更新用户角色成功", true);
    }
    
    @ApiOperation("批量设置用户角色")
    @PostMapping("/batch")
    @Transactional
    public Result<Boolean> batchSetUserRoles(@RequestBody UserRoleDTO userRoleDTO) {
        Integer userId = userRoleDTO.getUserId();
        List<Integer> roleIds = userRoleDTO.getRoleIds();
        
        // 首先移除用户的所有角色
        List<Role> userRoles = roleService.getRolesByUserId(userId);
        for (Role role : userRoles) {
            roleService.removeRoleFromUser(userId, role.getRoleId());
        }
        
        // 然后添加新的角色
        if (roleIds != null && !roleIds.isEmpty()) {
            for (Integer roleId : roleIds) {
                roleService.assignRoleToUser(userId, roleId);
            }
        }
        
        return Result.success("批量设置用户角色成功", true);
    }
} 